//
//  BGOwnInviteBidLogsVC.m
//  bigMall
//
//  Created by LTOVE on 16/8/22.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGOwnInviteBidLogsVC.h"
#import "BGOwnInviteBidLogsCell.h"
#import "BGOwnInviteBidLogsModel.h"
#import "MJRefresh.h"
#import "BGDateResult.h"
#import "BGOwnInviteBidLogsResultModel.h"
#import "BGReplyBidModel.h"
@interface BGOwnInviteBidLogsVC ()<BGOwnInviteBidLogsCellDeleGate>{
    
    NSMutableArray *_dateArray;
    NSUInteger _thisPage;
    NSUInteger _allPage;
}

@end

@implementation BGOwnInviteBidLogsVC

NSString *const page = @"10";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView.header beginRefreshing];
}

- (void)loadData
{
    [BGDateResult ownInviteBidLogsWithThisPage:@"1" pageNum:page inviteld:self.inviteld success:^(NSObject *result) {
        
        
        [self.tableView.header endRefreshing];
        BGOwnInviteBidLogsResultModel *Model = (BGOwnInviteBidLogsResultModel *)result;
        
        if ([Model.allPage integerValue] > [page integerValue]) {
            self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDate)];
        }
        _dateArray = [[NSMutableArray alloc]initWithArray:Model.list];
        [self.tableView reloadData];

        _thisPage = 1;
        _allPage = [Model.allPage integerValue];
        if (Model.list.count) {
            _thisPage ++;
        }
        
    } failure:^(NSError *error) {
        
        
    }];
}

- (void)loadMoreDate
{
    if (_thisPage > _allPage) {
        [self.tableView.footer endRefreshingWithNoMoreData];
        return;
    }
    [BGDateResult ownInviteBidLogsWithThisPage:[NSString stringWithFormat:@"%ld",_thisPage] pageNum:page inviteld:@"26" success:^(NSObject *result) {
        BGOwnInviteBidLogsResultModel *resultModel = (BGOwnInviteBidLogsResultModel *)result;
        [_dateArray addObjectsFromArray:resultModel.list];
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
        if (resultModel.list.count) {
            _thisPage ++;
        }
        
    } failure:^(NSError *error) {
        
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return _dateArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 81;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BGOwnInviteBidLogsCell *cell = [BGOwnInviteBidLogsCell cellWithTableView:tableView];
    
    BGOwnInviteBidLogsModel *model = _dateArray[indexPath.row];
    model.tag = indexPath.row;
    cell.delegate = self;
    cell.model = model;
    
    // Configure the cell...
    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}



/**
 *  按钮点击代理
 *
 *  @param btn btn description
 */
- (void)bidLogBtnClick:(UIButton *)btn{
    NSString *reply;
    if ([btn.titleLabel.text isEqualToString:@"接受"]) {
        reply = @"yes";
    }else{
        reply = @"no";
    }
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"确定%@吗？",btn.titleLabel.text] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        BGOwnInviteBidLogsModel *model = _dateArray[btn.tag];
        [BGDateResult replyBidWithInviteld:model.invite_id bidId:model.idStr reply:reply success:^(NSObject *result) {
            
            BGReplyBidModel *model = (BGReplyBidModel *)result;
            GWLog(@"%@",model.msg);
            if ([model.status isEqualToString:@"1"]) {
                //成功更改 刷新列表
//                BGOwnInviteBidLogsModel *dataModel = _dateArray[btn.tag];
//                dataModel.result_str = [NSString stringWithFormat:@"已%@",btn.titleLabel.text];
//                [_dateArray replaceObjectAtIndex:btn.tag withObject:dataModel];
//                NSIndexPath *index = [NSIndexPath indexPathForRow:btn.tag inSection:0];
//                [self.tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
                [self.tableView.header beginRefreshing];
                
            }
            
            
        } failure:^(NSError *error) {
            
            
        }];
        
    }];
    [alertVC addAction:cancel];
    [alertVC addAction:sure];
    [self presentViewController:alertVC animated:YES completion:^{
        
        
    }];

}
@end
